# from django.shortcuts import render
#
# # Create your views here.
# from rest_framework.response import Response
# from rest_framework.views import APIView
#
#
# class Test(APIView):
#     def get(self,request):
#         return Response("ok")

from django.http import HttpResponse
from django.shortcuts import render

from django.views.generic import View
from .forms import *


class IndexView(View):
    # 如果是GET请求，直接渲染到上传文件页面
    def get(self, request):
        pass

import random, re
from rest_framework.response import Response
from rest_framework.views import APIView
from utils.comm import send_message, get_redis
from users.models import *


# 测试
class Text(APIView):
    def get(self, request):
        conn = get_redis()
        conn.setex('username', 10, 'xiaoming236')
        name = conn.get('username')
        return Response(name)


# 注册
class Register(APIView):
    def post(self, request):

        # 用户名
        username = request.data.get('username')
        if not username:
            return Response({'code': 10010})
        # 格式是否正确
        name = re.match('\w{5,10}$', username)
        if not name:
            return Response({'code': 10011})
        # 是否唯一
        user = User.objects.filter(username=username).first()
        if user:
            return Response({'code': 10012})

        # 密码
        password = request.data.get('password')
        if not password:
            return Response({'code': 10013})
        # 格式是否正确
        pwd = re.match('\w{3,6}$', password)
        if not pwd:
            return Response({'code': 10014})

        # 手机号
        mobile = request.data.get('mobile')
        if not mobile:
            return Response({'code': 10016})
        phone = re.match('^1[3-9]\d{9}$', mobile)
        if not phone:
            return Response({'code': 10017})
        # 是否唯一
        sloe = User.objects.filter(mobile=mobile).first()
        if sloe:
            return Response({'code': 10018})

        # 验证码验证
        smscode = request.data.get('smscode')
        conn = get_redis()
        code = conn.get(mobile)
        if code:
            code = str(code, encoding='utf8')
        if smscode != code:
            return Response({'code': 10019})

        return Response({'code': 200, 'meg': '注册成功'})


# 验证码
class SendSms(APIView):
    def post(self, request):
        mobile = request.data.get('mobile')
        codesms = random.randint(1000, 9999)
        res = send_message(mobile, str(codesms), 1)
        code = 10010
        if res:
            code = 200
            # 发生成功后存入redis
            conn = get_redis()
            conn.setex(mobile, 60, codesms)
        return Response({'code': code})

    from django.shortcuts import render
    from .models import *
    from rest_framework.response import Response
    from rest_framework.views import APIView
    from .ser import *
    # Create your views here.
    import re

    class UserM(APIView):
        def post(self, request):
            ser = Userser(data=request.data)
            if ser.is_valid():
                ser.save()
                return Response({'code': 200})
            else:
                print(ser.errors)
                return Response({'code': 20010})

    class Usermanage(APIView):
        def post(self, request):
            username = request.data.get('username')
            password = request.data.get('password')
            user = User.objects.filter(username=username).first()
            if user:
                if user.password == password:
                    return Response({'code': 200, 'userid': user.id})
                else:
                    return Response({'code': 20010, 'meg': '密码错误'})
            else:
                return Response({'code': 20011, 'meg': '用户不存在'})

    class NewsM(APIView):
        def post(self, request):
            ser = Newser(data=request.data)
            if ser.is_valid():
                ser.save()
                return Response({'code': 200})
            else:
                print(ser.errors)
                return Response({'code': 20010})

        def get(self, request):
            se = News.objects.all()
            ser = Newser(se, many=True)
            return Response({'code': 200, 'list': ser.data})